<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>杭州分店管理</el-breadcrumb-item>
            <el-breadcrumb-item>新增房间</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
        <el-form  :model="form" ref="form" :rules="rules" label-width="110px" style="width:500px;" >
            <el-form-item label="房间照片" prop="picture">
                <el-upload
                    name="avatar"
                    class="avatar-uploader"
                    action="http://localhost:9000/upload"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess">
                    <img v-if="form.picture" :src="form.picture" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="房间类型名称" prop="roomTypeName">
                <el-select v-model="form.roomTypeName" placeholder="请选择房间类型">
                    <el-option v-for="item in roomType" :key="item.typeId" :label="item.typeName" :value="item.typeName"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="房间价格" prop="roomPrice">
                <el-input   v-model="form.roomPrice" placeholder="请输入房间价格"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="addRoom" type="primary" round>新增</el-button>
                <el-button @click="resetForm" type="success" round>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                roomType:[],
                form: {
                    roomTypeName:'',
                    roomPrice:'',
                    isUsed:'否',
                    picture:'',

                },
                rules:{
                    roomPrice:[{required:true,message:'房间价格必填',trigger:'blur'}],
                    roomTypeName:[{required:true,message:'房间类型必填',trigger:'blur'}],
                    picture:[{required:true,message:'房间照片必填',trigger:'blur'}],
                }
            }
        },
        methods:{
            addRoom(){
                this.$refs['form'].validate(valid=>{
                    if(valid){
                        //console.log(this.form);
                        this.$http.hangzhouApi.add(this.form).then(res=>{
                        console.log(res);
                        if(res.data.code==200){
                            this.$message({
                                type:'success',
                                message:'杭州分店增加房间成功'
                            })
                            this.$router.push('/home/hangzhou-list')
                        }
                        }).catch(err=>console.log(err))
                    }else{
                        this.$notify({
                            type:'error',
                            message:'表单填写错误！'
                        })
                    }
                })
            },
            resetForm(){
                this.$refs.form.resetFields();
            },
            handleAvatarSuccess(res,file){
                console.log(res);
                this.form.picture='http://localhost:9000/'+res.filename
                
                
            },
        },
        mounted(){
            //加载房间种类下拉选的内容
            this.$http.typeApi.queryAll().then(res=>{
                console.log(res);
                this.roomType=res.data.data
            }).catch(err=>console.log(err))
        }
    }
</script>

<style lang="scss" >
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>